package com.xxkk.spring_boot_web.utils;

import org.springframework.stereotype.Component;

import java.io.Closeable;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author xxkk
 * @version 1.0.0
 * @title BusinessPool
 * @description ʕ•ᴥ•ʔ
 * @create 2023/11/13 14:07
 **/
@Component
public class BusinessPool implements Closeable {
    private ScheduledThreadPoolExecutor executor;
//    private ExecutorService executorService;

    public ScheduledThreadPoolExecutor getExecutor() {
        return executor;
    }
//    public ExecutorService getExecutorService(){
//        return executorService;
//    }

    public static class BusinessPoolThreadFactory implements ThreadFactory {
        final ThreadGroup group = Thread.currentThread().getThreadGroup();
        final AtomicInteger threadNumber = new AtomicInteger(1);
        final String namePrefix = "business-pool";

        public Thread newThread(Runnable r) {
            Thread t = new Thread(group, r, namePrefix + threadNumber.getAndIncrement(), 0);
            if (t.isDaemon())
                t.setDaemon(false);
            if (t.getPriority() != Thread.NORM_PRIORITY)
                t.setPriority(Thread.NORM_PRIORITY);
            return t;
        }
    }

    public BusinessPool() {
        this.executor = new ScheduledThreadPoolExecutor(1, new BusinessPoolThreadFactory());
    }
//    public BusinessPool(int _2){
//        this.executorService = new ThreadPoolExecutor(
//                2,
//                Runtime.getRuntime().availableProcessors() * 2,
//                10,
//                TimeUnit.SECONDS,
//                new ArrayBlockingQueue<>(100),
//                new ThreadPoolExecutor.CallerRunsPolicy()
//        );
//    }

    private static BusinessPool instance = null;

    public static BusinessPool Ins() {
        if (instance == null) {
            synchronized (BusinessPool.class) {
                if (instance == null) {
                    instance = new BusinessPool();
                }
            }
        }
        return instance;
    }

    public void execute(Runnable runnable) {
        executor.execute(runnable);
    }

    /**
     * xxkk 20250106
     * 服务器关闭时自动执行关闭线程池
     */
    public void close() {
        System.out.println("Server Pool closing...");
        if (executor != null) {
            executor.shutdownNow();
        }
    }

    public void schedule(Runnable runnable, long delay, TimeUnit unit) {
        executor.schedule(runnable, delay, unit);
    }

    public ScheduledFuture<?> scheduleAtFixRate(Runnable runnable, long initialDelay, long period, TimeUnit unit) {
        return executor.scheduleAtFixedRate(runnable, initialDelay, period, unit);
    }

    public ScheduledFuture<?> scheduleWithFixedDelay(Runnable runnable, long initialDelay, long period, TimeUnit unit) {
        return executor.scheduleWithFixedDelay(runnable, initialDelay, period, unit);
    }

    public <T> Future<T> submit(Callable<T> callable) {
        return this.executor.submit(callable);
    }
}
